/**
 * 
 */
package edu.arizona.eller.mis.liferoulette;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import edu.arizona.eller.mis.liferoulette.db.dao.UserPreferenceDAO;
import edu.arizona.eller.mis.liferoulette.entity.UserPreference;

/**
 * @author Blaine Boule - blaineb@email.arizona.edu
 *
 */
public class UserPreferenceAcvitity extends Activity implements OnClickListener {
	private EditText miles,name;
	private CheckBox useFacebook,useSMS;
	private Button bSave;
	
	public void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_preferences); 
		
		bSave = (Button)this.findViewById(R.id.saveButtonP);
		bSave.setOnClickListener(this);
		
		//get original setting
		UserPreferenceDAO dao = new UserPreferenceDAO(getApplicationContext());
		SharedPreferences myPrefs = this.getSharedPreferences("lrPrefs", MODE_PRIVATE);
	    String userName = myPrefs.getString("un_pref", null);
        
		miles = (EditText)this.findViewById(R.id.miles);
		name = (EditText)this.findViewById(R.id.displayNameText);
		name.setText(userName);
		//miles.setOnKeyListener(this);
		useFacebook = (CheckBox)this.findViewById(R.id.useFacebook);
		useFacebook.setOnClickListener(this);
		useSMS = (CheckBox)this.findViewById(R.id.useSMS);
		useSMS.setOnClickListener(this);

		UserPreference up = null;
		//Set miles value
		up = dao.findById(1);
		if(up != null)
			miles.setText(up.getValue());
		//Set facebook Checkbox
		up = dao.findById(2);
		if(up != null)
			useFacebook.setChecked(new Boolean(up.getValue()));
		//Set SMS CheckBox
		up = dao.findById(3);
		if(up != null)
			useSMS.setChecked(new Boolean(up.getValue()));
		
		 if(userName == null){
		    Toast.makeText(this, "Please add a display name for Life Roulette...", Toast.LENGTH_LONG).show();
		 }
		
		dao.closeDAO();
	}
	
	public void saveSettings(View v){
		UserPreferenceDAO dao = new UserPreferenceDAO(getApplicationContext());
		UserPreference up = new UserPreference();
		if(v.getId() == R.id.miles){
			up.setId(1);
			up.setValue(miles.getText().toString());
		}else if(v.getId() == R.id.useFacebook){
			up.setId(2);
			if(useFacebook.isSelected() || useFacebook.isChecked()){
				up.setValue("true");
			}else{
				up.setValue("false");
			}
		}if(v.getId() == R.id.useSMS){
			up.setId(3);
			if(useSMS.isSelected() || useSMS.isChecked()){
				up.setValue("true");
			}else{
				up.setValue("false");
			}
		}
		dao.update(up);
		dao.closeDAO();
		
        SharedPreferences myPrefs = this.getSharedPreferences("lrPrefs", MODE_PRIVATE);
        SharedPreferences.Editor editor = myPrefs.edit();
        editor.putString("un_pref", name.getText().toString());
        editor.commit();
	}

	public void onClick(View v) {
		saveSettings(v);
		this.finish();
	}

//	public boolean onKey(View v, int keyCode, KeyEvent event) {
////		if(keyCode == KeyEvent.ACTION_UP)
////			saveSettings(v);
////		return true;
//	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.preferences_menu, menu);
	    return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.historyItem:     
	        	startActivity(new Intent(this, HistoryActivity.class));
	            break;
	        case R.id.homeItem:     
	        	startActivity(new Intent(this, main.class));
	            break;
	    }
	    return true;
	}
}
